package com.server.server_a10.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.server.server_a10.entity.Home;
import com.server.server_a10.entity.HomeUserConnect;
import com.server.server_a10.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface HomeMapper extends BaseMapper<Home> {

    //获取用户的主房子
    @SelectProvider(type = ProviderHome.class, method = "getMainHomeByUserId")
    int getMainHomeByUserId(int userId);

    /**
     * 返回用户名下的房子
     * @param master_id
     * @return
     */
    @SelectProvider(type = ProviderHome.class, method = "getHomeByUserId")
    List<Home> getHomeByUserId(long master_id);

    @SelectProvider(type = ProviderHome.class, method = "getRelateHomeByUserId")
    List<Home> getRelateHomeByUserId(long user_id);


    @SelectProvider(type = ProviderHome.class, method = "getHomeLimit")
    List<Home> getHomeLimit(@Param("index") int index,
                            @Param("limit") int limit,
                            @Param("name") String name);

    @SelectProvider(type = ProviderHome.class, method = "getHomeCount")
    int getHomeCount();

    @DeleteProvider(type = ProviderHome.class, method = "delHomeById")
    int delHomeById(@Param("id") int id);

    @InsertProvider(type = ProviderHome.class, method = "insHome")
    int insHome(@Param("name") String name,
                @Param("master_id") int master_id,
                @Param("address") String address);

    //通过房子的id，获取房子内所有房间的id
    @SelectProvider(type = ProviderHome.class,method = "selRoomByHomeId")
    List<Integer> selRoomByHomeId(@Param("homeId") int homeId);

    class ProviderHome {
        public static String getMainHomeByUserId(int userId){
            return "select home_id from user_main_home where user_id=#{userId}";
        }

        public static String getHomeByUserId() {
            return "select * from home where master_id=#{master_id}";
        }
        public static String getRelateHomeByUserId(){
            return "select * from home where id in (select home_id from home_user_connect where user_id=#{user_id});";
        }

        //根据id和limit，index来获取用户信息（需要的name参数实际上是用户的id）
        public static String getHomeLimit(int index,int limit,String name){
            return "select id,name,master_id,address,\n" +
                    "       (select count(*) from room where home_id=home.id) as room_count from home " +
                    "where id like CONCAT('%',#{name},'%') limit #{index},#{limit}";
        }

        public static String getHomeNameLimit(int index,int limit){
            return "SELECT h.id, h.name, h.master_id, h.address, COUNT(r.id) AS room_count\n" +
                    "FROM home h\n" +
                    "         LEFT JOIN room r ON h.id = r.home_id\n" +
                    "GROUP BY h.id limit #{index},#{limit}";
        }

        public static String getHomeCount(){
            return "select count(*) from home";
        }

        public static String delHomeById(int id){
            return "delete from home where id=#{id}";
        }

        public static String insHome(String name,int master_id,String address){
            return "insert into home (name, create_time, update_time, master_id, address)\n" +
                    "values (#{name},now(),now(),#{master_id},#{address})";
        }
        public static String selRoomByHomeId(int homeId){
            return "select r.id as roomId\n" +
                    "from home\n" +
                    "         left join room r on home.id = r.home_id\n" +
                    "where home.id = #{homeId}";
        }
    }
}
